package com.acmen.ump.adminapi.controller.sys;


import com.acmen.ump.adminapi.constant.EnumMenuLevel;
import com.acmen.ump.adminapi.response.MenuTreeDto;
import com.acmen.ump.core.dto.ApiResponse;
import com.acmen.ump.entity.sys.SysMenu;
import com.acmen.ump.service.sys.ISysMenuService;
import com.acmen.ump.service.sys.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationPid;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
    * 系统菜单表 前端控制器
    * </p>
 *
 * @author ftpolaris
 * @since 2019-09-01
 */
@RestController
@RequestMapping("/sys/sys-menu")
public class SysMenuController {
    @Autowired
    private ISysMenuService iSysMenuService;


    @GetMapping("/menu-tree")
    public ApiResponse getMenuTree(){
        List<MenuTreeDto> menuTreeDtos = new ArrayList<>();
        List<SysMenu> menuList = iSysMenuService.list();
        List<SysMenu> firstMenuList = menuList.stream()
                .filter(x->EnumMenuLevel.MENU_LEVEL_FIRST.match(x.getLevel())
                        && null == x.getPId())
                .collect(Collectors.toList());

        for (SysMenu firstMenu : firstMenuList){
            MenuTreeDto menuTreeDto = new MenuTreeDto(firstMenu);
            List<SysMenu> secondMenuList = menuList.stream()
                    .filter(x->firstMenu.getId().equals(x.getPId()))
                    .collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(secondMenuList)){
                List<MenuTreeDto> secondMenuTreeDtos = new ArrayList<>();
                for(SysMenu secondMenu : secondMenuList){
                    MenuTreeDto secondMenuTreeDto = new MenuTreeDto(secondMenu);
                    List<SysMenu> thirdMenuList = menuList.stream()
                            .filter(x->secondMenu.getId().equals(x.getPId()))
                            .collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(thirdMenuList)){
                        List<MenuTreeDto> thirdMenuTreeDtos = thirdMenuList.stream()
                                .map(x->new MenuTreeDto(x))
                                .collect(Collectors.toList());
                        secondMenuTreeDto.setChildren(thirdMenuTreeDtos);
                    }
                    secondMenuTreeDtos.add(secondMenuTreeDto);
                }
                menuTreeDto.setChildren(secondMenuTreeDtos);
            }

            menuTreeDtos.add(menuTreeDto);
        }

        return new ApiResponse(menuTreeDtos);
    }

    @PostMapping("/add")
    public ApiResponse add(SysMenu sysMenu){
        iSysMenuService.save(sysMenu);
        return new ApiResponse();
    }

    @PostMapping("/update")
    public ApiResponse update(SysMenu sysMenu){
        iSysMenuService.updateById(sysMenu);
        return new ApiResponse();
    }

    @PostMapping("/delete")
    public ApiResponse delete(Long[] ids){
        iSysMenuService.removeByIds(Arrays.asList(ids));
        return new ApiResponse();
    }
}
